/**
 * Created by yuji on 2015/10/28.
 */

'use strict'
var util = require('./util');

function ResourceMap (map){
    this.map = map || {
            res:{}
        }
}

//静态方法
ResourceMap.initFromObj = function(obj){
    let res = obj['res'];
    for(let key in res){
        if(util.hasProp(res, key)){
            res[key]["deps"] = new Set(res[key]["deps"] );
        }
    }
    return new ResourceMap(obj);

}

ResourceMap.prototype.uri = function(name){
    return this.map.res[name]['uri'];
}

ResourceMap.prototype.setUri = function(name, uri){
    if(this.map.res.hasOwnProperty(name)){
       this.map.res[name]['uri'] = uri;
    }
    else{
        throw new Error('设置资源表中的uri时，引用了未声明的资源名');
    }
}

/**
 * 使用了es6中的Set数据结构
 * 这货不能被json。stringify
 * @param name
 * @param dep
 */
ResourceMap.prototype.addDep = function(name, dep){
    if(this.map.res.hasOwnProperty(name)){
        this.map.res[name].deps.add(dep);
    }
    else{
        this.map.res[name] = {
            uri:'',
            deps: new Set()
        }
        this.addDep(name,dep);
    }

}

ResourceMap.prototype.deps = function(name){
    return this.map.res[name]['deps'];
}

ResourceMap.prototype.setDep = function(name, deps){
    if(this.map.res.hasOwnProperty(name)){
        this.map.res[name].deps = new Set(deps);
    }
    else{
        this.map.res[name] = {
            uri:'',
            deps: new Set()
        }
        this.setDep(name,deps);
    }
}

ResourceMap.prototype.removeDep = function(name, dep){
    this.map.res[name].deps.delete(dep);
}
/**
 * 将所有的set转化为普数组
 */
ResourceMap.prototype.toNormalObj = function(){
    let normalMapRes =  {

    };
    let res = this.map.res;
    for(let key in res){
        if({}.hasOwnProperty.call(res,key)){
            normalMapRes[key] = {
                uri:res[key]['uri'],
                deps: util.set2array(res[key]['deps'])
            };
        }
    }
    this.normalMap = {
        "res": normalMapRes
    }
}

ResourceMap.prototype.save = function(name){
    util.newFile(name?name:'map.json',null,JSON.stringify(this.normalMap));
}

//转化为只含uri的map，方便模板引擎的调用
ResourceMap.prototype.toUriMap = function(){
    var res = this.map.res;
    var obj = this.uriMap || {};
    for(var key in res){
        if(util.hasProp(res, key)&&(!obj[key])){
            obj[key] = res[key]['uri'];
        }
    }
    this.uriMap = obj;
    return obj;
}

exports.ResourceMap = ResourceMap;
//var str = '{"res":{"image/icon.css":{"uri":"/static/image/icon.css","deps":["image/icon0.png","image/icon2.png"]}}}'
//var rm = ResourceMap.initFromObj(JSON.parse(str));
//
////rm.addDep('image/icon.css','image/icon0.png');
////rm.addDep('image/icon.css','image/icon2.png');
////
////rm.setUri('image/icon.css','/static/image/icon.css')
//rm.toNormalObj();
//
//console.log(JSON.stringify(rm.normalMap));


function ReverseMap (obj){
    this.map = obj || {};
}
/**
 * 由资源表生成静态资源表
 * @param resourceMap
 */
ReverseMap.genByReourceMap = function(resourceMap){
    var res = resourceMap.map.res;
    var obj = new ReverseMap();
    for(var key in res){
        if(util.hasProp(res, key)){
            for(let item of res[key]['deps']){
                obj.addDep(item, key);
            }
        }
    }
    return obj;
}
//不需要这个方法
//ReverseMap.initFromObj = function(obj){
//    for(let key in obj){
//        if(util.hasProp(obj, key)){
//            obj[key] = new Set(obj[key]);
//        }
//    }
//    return new ReverseMap(obj)
//}

ReverseMap.prototype.addDep = function(name, depBy){
    if(util.hasProp(this.map,name)){
        Set.prototype.add.call(this.map[name],depBy);
    }
    else{
        this.map[name] = new Set();
        this.addDep(name, depBy);
    }
}

ReverseMap.prototype.removeDep = function (name, depBy) {
    Set.prototype.delete.call(this.map[name], depBy);
}

ReverseMap.prototype.toNormalObj = function(){
    this.normalMap = {};
    for(let key in this.map){
        if(util.hasProp(this.map, key)){
            let arr = [];
            for(let item of this.map[key]){
                arr.push(item);
            }
            this.normalMap[key] = arr;
        }
    }
}

//不需要这个方法
//ReverseMap.prototype.save = function(){
//    ResourceMap.prototype.save.call(this, 'map_reverse.json')
//};

exports.ReverseMap = ReverseMap;

//var rm = new ReverseMap();
//
//rm.addDep('a','b');
//rm.addDep('a','c');
//rm.removeDep('a','b')
//rm.toNormalObj();
//console.log(rm.normalMap)